var base = require('/base_info.js');
var utils = require('/utils/index.js');
Page({
  data: {
    cardInfo: {},
    imgUrl: base.imgUrl,
    addCardSign: false,
    apply_card_url: ''
  },
  // 返回首页
  home () {
    my.navigateBack({
      delta: 100
    });
  },
  // 跳转到房型列表页面
  use () {
    my.redirectTo({
      url: './../room/room-list'
    });
  },
  // 加入卡包
  addCardClick () {
    let that = this;
    console.log(that.data.apply_card_url);
    if (!that.data.apply_card_url || that.data.apply_card_url == '') {
      my.alert({
        content: that.data.apply_card_url
      });
      return false;
    };
    my.showLoading();
    new Promise((resolve, reject) => {
      my.addCardAuth({
        url: that.data.apply_card_url,
        success: (res) => {
          if (res.success) {
            let data = res.result;
            data.external_card_no = that.data.cardInfo.card_no;
            resolve(data);
          } else {
            reject(1);
          };          
        },
        fail: () => {
          reject(1);
        }
      })
    }).then((data) => {
      // 加入卡包
      utils.getData('CardOpen', data, (res) => {
        my.hideLoading();
        if (res.data.message == 'success') {
          my.alert({
            content: '已成功加入卡包'
          });
          that.setData({
            addCardSign: !that.data.addCardSign
          });
          return false;
        }
        my.alert({
          content: '加入卡包失败'
        });
      }, (err) => {
        my.hideLoading();
        my.alert({
          content: '加入卡包失败'
        });
        console.log(err);
      });
    }, (err) => {
      my.hideLoading();
      console.log(err);
    });
  },
  // 获取卡片详情
  getData (id) {
    let that = this;
    new Promise((resolve, reject) => {
      utils.getData('SingleMyCard', {
        page: 1,
        size: 10,
        user_id: my.getStorageSync({
          key: 'uid', // 缓存数据的key 
        }).data,
        type: 1
      }, (res) => {
        if (res.data.errno > 0) {
          that.setData({
            cardInfo: res.data.data[0]
          });
          resolve(1);
          return false;
        };
        reject(2);
      }, (err) => {
        reject(2);
      });
    }).then((data) => {
      // 获取加入卡包的链接地址
      utils.getData('Activateurl', {
        template_id: that.data.cardInfo.template_id ? that.data.cardInfo.template_id : ''
      }, (res) => {
        my.hideLoading();
        that.setData({
          apply_card_url: res.data.data.apply_card_url
        });
      }, (err) => {
        my.hideLoading();
        console.log(err);
      });
    }, (err) => {
      my.hideLoading();
    });
  },
  onLoad(query) {
    my.showLoading();
    this.getData(query.id);
  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: my.getStorageSync({
        key: 'hotel_name', // 缓存数据的key
      }).data,
      desc: my.getStorageSync({
        key: 'my_app_description', // 缓存数据的key
      }).data,
      path: 'pages/index/index?share=true',
      success: function (e) {
        console.log(e);
      }
    };
  },
});
